package model;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Vector;

public class Items {

	Vector<ServerShape> shapes = new Vector<ServerShape>();

	public Items() {
		super();
		defaultPopulate();
	}

	public void defaultPopulate() {
		shapes.add(new Line(247, 282, 256, 159));
		shapes.add(new Line(113, 157, 245, 298));
		shapes.add(new Line(236, 312, 102, 160));
		shapes.add(new Oval(128, 235, 148, 266));
		shapes.add(new Oval(118, 145, 9, 48));
		shapes.add(new Line(249, 297, 30, 79));
		shapes.add(new Line(384, 394, 75, 122));
		shapes.add(new Line(245, 341, 154, 142));
		shapes.add(new Rectangle(13, 456, 8, 377));
		shapes.add(new Line(185, 230, 38, 84));
		shapes.add(new Line(168, 207, 82, 75));
		shapes.add(new Line(147, 150, 60, 77));
		shapes.add(new Oval(72, 74, 162, 115));
		shapes.add(new Oval(58, 107, 150, 124));
		shapes.add(new Oval(93, 99, 164, 211));
		shapes.add(new Oval(47, 47, 199, 235));
	}

	public ServerShape addItem(String args) {
		ServerShape shape = null;
		if (args.contains("Smily"))
			shape = new Smily();
		else if (args.contains("Line"))
			shape = new Line();
		else if (args.contains("Oval"))
			shape = new Oval();
		else if (args.contains("Rectangle"))
			shape = new Rectangle();
		else if (args.contains("SelectBox"))
			shape = new SelectBox();
		else if (args.contains("Move"))
			shape = new Move();
		if (shape != null) {
			shape.parse(args);
			shapes.add(shape);
			System.out.println("shape created " + args);
			return shape;
		} else
			return null;
	}

	
	
	//spécification du protocole:
	
	//ajouter une forme (Smily, Rectangle, Line, Oval...) à la position x1;y1;x2;y2
	//req: p_create_shape 10;10;10;10;;;;Oval
	//rép: added: 10;10;10;10;10;10;notsel;Oval
	
	//obtenir le nombre de formes
	//req: p_get_shape_count
	//rép: count: 18
	
	//obtenir la forme à l'indice n
	//req: p_get_shape 12
	//rép: shape at index 12: 72;74;162;115;72;162;notsel;Oval
	
	//obtenir la dernière forme de la collection
	//req: p_get_last_shape
	//rép: last shape: 10;10;10;10;10;10;notsel;Oval
	
	//obtenir toutes les formes
	//req: p_get_all_shapes
	//rép: all shapes: 247;282;256;159;247;256;notsel;Line---113;157;245;298;113;245;notsel;Line etc...
	
	
	public boolean parseCommand(String mesg, Socket client) throws IOException {
		if (mesg.contains("p_create_shape")) {
			String args = mesg.substring("p_create_shape".length()).trim();
			ServerShape added = addItem(args);
			PrintStream out = new PrintStream(client.getOutputStream());
			out.println("added: " + added.asString());
			return true;
		} else if (mesg.contains("p_get_shape_count")) {
			PrintStream out = new PrintStream(client.getOutputStream());
			out.println("count: " + shapes.size());
			return true;
		} else if (mesg.contains("p_get_shape")) {
			String args = mesg.substring("p_get_shape".length()).trim();
			int index = Integer.parseInt(args);
			PrintStream out = new PrintStream(client.getOutputStream());
			out.println("shape at index " + index + ": "
					+ shapes.get(index).asString());
			return true;
		} else if (mesg.contains("p_get_last_shape")) {
			PrintStream out = new PrintStream(client.getOutputStream());
			out.println("last shape: "
					+ shapes.get(shapes.size() - 1).asString());
			return true;
		} else if (mesg.contains("p_get_all_shapes")) {
			sendItems(client);
			return true;
		} else
			return false;
	}



	public void sendItems(Socket client) throws IOException {
		String mesg = "all shapes: ";
		for (ServerShape shape : shapes) {
			mesg += shape.asString() + "---";
		}
		PrintStream out = new PrintStream(client.getOutputStream());
		out.println(mesg);
	}
	


}
